package com.tarena.lbs.user.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AdminRepositoryImpl implements AdminRepository {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public AdminPO save(AdminPO admin) {
        adminMapper.insert(admin);
        return admin;
    }

    @Override
    public AdminPO findByPhone(String phone) {
        LambdaQueryWrapper<AdminPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminPO::getAccountPhone, phone);
        return adminMapper.selectOne(wrapper);
    }

    @Override
    public AdminPO getAdminByPhone(String phone) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_phone",phone);
        return adminMapper.selectOne(queryWrapper);
    }

    @Override
    public AdminPO getAdminById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminPO> adminPageList(AdminQuery query) {
        QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(query.getNickname())) {
            queryWrapper.like("nickname", query.getNickname());
        }
        if (!StringUtils.isNullOrEmpty(query.getAccountPhone())) {
            queryWrapper.eq("account_phone", query.getAccountPhone());
        }
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<AdminPO> adminPOS = adminMapper.selectList(queryWrapper);
        return new PageInfo<>(adminPOS);
    }


}